using System;
using ICSharpCode.TextEditor.Gui.CompletionWindow;

namespace ICSharpCode.TextEditor.Formatting.Intellisense
{
    public class DefaultCompletionData : ICompletionData
    {
        protected string text;

        protected string description;

        protected int imageIndex;

        private double priority;

        public int ImageIndex => imageIndex;

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public virtual string Description => description;

        public double Priority
        {
            get
            {
                return priority;
            }
            set
            {
                priority = value;
            }
        }

        public virtual bool InsertAction(TextArea textArea, char ch)
        {
            string wordBeforeCaret = textArea.GetWordBeforeCaret();
            Caret caret = textArea.Caret;
            int num = caret.Offset + 1;
            int num2 = num;
            if (!string.IsNullOrEmpty(wordBeforeCaret))
            {
                num -= wordBeforeCaret.Length + 1;
                num2--;
                if (num2 - num > 0)
                {
                    textArea.Document.Remove(num, num2 - num);
                }
                textArea.Caret.Position = textArea.Document.OffsetToPosition(Math.Min(num, textArea.Document.TextLength));
            }
            textArea.InsertString(text);
            return false;
        }

        public DefaultCompletionData(string text, int imageIndex)
        {
            this.text = text;
            this.imageIndex = imageIndex;
        }

        public DefaultCompletionData(string text, string description, int imageIndex)
        {
            this.text = text;
            this.description = description;
            this.imageIndex = imageIndex;
        }

        public static int Compare(ICompletionData a, ICompletionData b)
        {
            if (a == null)
            {
                throw new ArgumentNullException("a");
            }
            if (b == null)
            {
                throw new ArgumentNullException("b");
            }
            return string.Compare(a.Text, b.Text, StringComparison.InvariantCultureIgnoreCase);
        }
    }
}
